package com.telenav.ui.widget;


import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.telenav.app.R;

public class ActionBar extends RelativeLayout implements OnClickListener {

    private LayoutInflater mInflater;
    
    private RelativeLayout mBarView;
    
    private ImageButton mLeftBtn;
    
    private RelativeLayout mLeftLayout;
    
    private View mBackIndicator;
    
    private TextView mTitleView;
    
    private LinearLayout mActionsView;
    
    private ProgressBar mProgress;
    
	public ActionBar(Context context, AttributeSet attrs) {
		super(context, attrs);

        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mBarView = (RelativeLayout) mInflater.inflate(R.layout.yy_actionbar, null);
        addView(mBarView);

        mLeftLayout = (RelativeLayout) mBarView.findViewById(R.id.actionbar_left_bg);
        mLeftBtn = (ImageButton) mBarView.findViewById(R.id.actionbar_left_btn);
        mBackIndicator = mBarView.findViewById(R.id.actionbar_back);

        mTitleView = (TextView) mBarView.findViewById(R.id.actionbar_title);
        mActionsView = (LinearLayout) mBarView.findViewById(R.id.actionbar_actions);
        
        mProgress = (ProgressBar) mBarView.findViewById(R.id.actionbar_progress);

        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ActionBar);
        CharSequence title = a.getString(R.styleable.ActionBar_title);
        if (title != null) {
        	mTitleView.setText(title);
        }
        a.recycle();
	}
	
	public void setTitle(String title){
		mTitleView.setText(title);
	}
	
    public void setBackEnabled(boolean show) {
        mBackIndicator.setVisibility(show? View.VISIBLE : View.GONE);
    }
    
    public void setProgressBarVisibility(int visibility) {
        mProgress.setVisibility(visibility);
    }
    
    public int getProgressBarVisibility() {
        return mProgress.getVisibility();
    }

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
        final Object tag = view.getTag();
        if (tag instanceof Action) {
            final Action action = (Action) tag;
            action.getClickListener().onClick();
        }
	}
	
    public void addLeftAction(Action action) {
    	mLeftBtn.setOnClickListener(this);
        mLeftBtn.setTag(action);
        mLeftBtn.setImageResource(action.getDrawable());
        mLeftLayout.setVisibility(View.VISIBLE);
    }
	
    /**
     * Adds a new {@link Action}.
     * @param action the action to add
     */
    public void addRightAction(Action action) {
        final int index = mActionsView.getChildCount();
        addAction(action, index);
    }

    /**
     * Adds a new {@link Action} at the specified index.
     * @param action the action to add
     * @param index the position at which to add the action
     */
    public void addAction(Action action, int index) {
        mActionsView.addView(inflateAction(action), index);
    }
    
    /**
     * Remove a action from the action bar.
     * @param action The action to remove
     */
    public void removeAction(Action action) {
        int childCount = mActionsView.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view = mActionsView.getChildAt(i);
            if (view != null) {
                final Object tag = view.getTag();
                if (tag instanceof Action && tag.equals(action)) {
                    mActionsView.removeView(view);
                }
            }
        }
    }
    
    /**
     * Removes all action views from this action bar
     */
    public void removeAllActions() {
        mActionsView.removeAllViews();
    }

    private View inflateAction(Action action) {
    	
        View view = mInflater.inflate(R.layout.yy_actionbar_item, mActionsView, false);

        ImageButton labelView = (ImageButton) view.findViewById(R.id.actionbar_item);
        labelView.setImageResource(action.getDrawable());

        view.setTag(action);
        view.setOnClickListener(this);
        return view;
    }
    
    
    public interface Action {
        public int getDrawable();
        public ActionOnClickListener getClickListener();
    }
    
    public interface ActionOnClickListener{
    	public void onClick();
    }

    public static class IntentAction implements Action {
    	
        final private int mDrawable;
        private ActionOnClickListener listener;

        public IntentAction(int drawable, ActionOnClickListener actionListener) {
            mDrawable = drawable;
            listener = actionListener;
        }

        @Override
        public int getDrawable() {
            return mDrawable;
        }
        
		@Override
		public ActionOnClickListener getClickListener() {
			return listener;
		}
    }
}
